package com.thot.timyag.web.rest;


import java.util.List;

import com.thot.timyag.core.HotDeployContext;
import com.thot.timyag.web.dao.entity.JarInfo;
import com.thot.timyag.web.pojo.ObjectRestResponse;
import com.thot.timyag.web.pojo.TableResultResponse;
import com.thot.timyag.web.service.JarInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author yangjf
 * @time 2019-04-10 9:23
 */
@RestController
@RequestMapping("/api/thot/v1")
public class ThotController {

    @Autowired
    private HotDeployContext context;

    @Autowired
    private JarInfoService jarInfoService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public TableResultResponse<JarInfo> list(@RequestParam("page") Integer page, @RequestParam("size") Integer size,
                                             @RequestParam("jarWorkspace") String jarWorkspace,
                                             @RequestParam(value = "description", required = false) String description
            , @RequestParam(value = "jarName", required = false) String jarName) {
        return jarInfoService.listPage(page, size, jarWorkspace, jarName, description);

    }

    @RequestMapping(value = "/updateDependence/{jarId}", method = RequestMethod.POST)
    @ResponseBody
    public ObjectRestResponse updateJar(@PathVariable("jarId") Integer jarId,
                                        @RequestParam(value = "file", required = false) MultipartFile multipartFile,
                                        @RequestParam("operater") String operater,
                                        @RequestParam("description") String description) {
        JarInfo jarInfo = jarInfoService.updateJar(jarId, multipartFile, operater, description);
        return new ObjectRestResponse().data(jarInfo);
    }

    @RequestMapping(value = "/uploadDependence", method = RequestMethod.POST)
    @ResponseBody
    public ObjectRestResponse uploadJar(@RequestParam("file") MultipartFile multipartFile,
                                        @RequestParam("jarWorkspace") String jarWorkspace,
                                        @RequestParam("operater") String operater,
                                        @RequestParam("description") String description) {
        JarInfo jarInfo = jarInfoService.addJarInfo(multipartFile, description, operater, jarWorkspace);
        return new ObjectRestResponse().data(jarInfo);
    }

    @RequestMapping(value = "/uninstallDependence/{jarId}", method = RequestMethod.DELETE)
    @ResponseBody
    public ObjectRestResponse uninstallDependenceById(@PathVariable("jarId") Integer jarId) {
        jarInfoService.deleteJarInfo(jarId);
        return new ObjectRestResponse<>();
    }

    @RequestMapping(value = "/uninstallDependence", method = RequestMethod.DELETE)
    @ResponseBody
    public ObjectRestResponse uninstallDependence(@RequestParam String jarName, @RequestParam String jarWorkspace) {
        jarInfoService.deleteJarInfo(jarName, jarWorkspace);
        return new ObjectRestResponse<>();
    }


    @RequestMapping(value = "/listDependence", method = RequestMethod.GET)
    @ResponseBody
    public List<String> listDependence() {
        return context.listDependence();
    }


    @RequestMapping(value = "/injectedBeans", method = RequestMethod.GET)
    @ResponseBody
    public List<String> viewInjectedBeans() {
        return context.getRegisteredBeans();
    }

    @RequestMapping(value = "/injectedApis", method = RequestMethod.GET)
    public TableResultResponse viewInjectedApis(@RequestParam(value = "page", required = false) Integer page,
                                                @RequestParam(value = "size", required = false) Integer size) {
        return jarInfoService.listApis(page, size);
    }

}
